#!/usr/bin/python3
# -*- coding: UTF-8 -*-

import datetime
import os
from auto_report.utils import info


class AutoReport():
    """auto process report"""

    def __init__(self, target_month):
        self.target_month = target_month
        [year, month] = self.target_month.split('-')
        this_month = datetime.date(int(year), int(month), 1)

    def get_month_field(self):
        [year, month] = self.target_month.split('-')
        this_month = datetime.date(int(year), int(month), 1)
        return f'{this_month.year}年{this_month.month}月'

    def get_last_month_field(self):
        [year, month] = self.target_month.split('-')
        last_month = datetime.date(int(year), int(
            month) - 1, 1) if int(month) > 1 else datetime.date(int(year) - 1, 12, 1)
        return f'{last_month.year}年{last_month.month}月'

    def get_files(self, folder):
        # folder is absolute path
        if not os.path.exists(folder):
            print(f'[bold red][错误] 文件夹{folder}不存在[/bold red]')
            quit()

        files = os.listdir(folder)
        info('开始处理：')
        xlsx_files = []
        for f in files:
            ext = os.path.splitext(f)
            if ext[1] == '.xlsx':
                xlsx_files.append(f)
            print(f'    {f}')
        return xlsx_files
